<?php
/**
* @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
* @license		GNU/GPL, see LICENSE.php
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/

// no direct access
defined('_JEXEC') or die('Restricted access');

//virtuemart parser
/*$path = JPATH_SITE.DS.'components'.DS.'com_virtuemart'.DS.'virtuemart_parser.php';

if (file_exists( $path )) {
	require_once( $path );
}*/

class modGavirtuemartHelper {
	
	// show the name of user
	function getCustomer($params)
	{
		$user = & JFactory::getUser();
		$show_user = $params->get( 'show_user', 0 );
		
		if ($show_user == 1 && !$user->get('guest'))
		{
			return 	"pageTracker._setVar(\"" .  $user->get('username') . "\");";
		}
		else
		{
			return "";
		}
		
	}
	
	//show a page view
	function getPageView($params)
	{
		
		if (JRequest::getVar( 'option' ) == "com_virtuemart" && JRequest::getVar( 'page' ) == "shop.registration")
		{
			$page = JText::_( 'Registration');
		}
		elseif (JRequest::getVar( 'option' ) == "com_virtuemart" && JRequest::getVar( 'page' ) == "shop.cart")
		{
			$page = JText::_( 'Cart');
		}
		elseif (JRequest::getVar( 'option' ) == "com_virtuemart" && JRequest::getVar( 'page' ) == "account.index")
		{
			$page = JText::_( 'Customer Account');
		}
		elseif (JRequest::getVar( 'option' ) == "com_virtuemart" && JRequest::getVar( 'page' ) == "account.billing")
		{
			$page = JText::_( 'Customer Information');
		}
		elseif (JRequest::getVar( 'option' ) == "com_virtuemart" && JRequest::getVar( 'page' ) == "account.shipping")
		{
			$page = JText::_( 'Shipping Addresses');
		}
		elseif (JRequest::getVar( 'option' ) == "com_virtuemart" && JRequest::getVar( 'page' ) == "account.shipto")
		{
			$page = JText::_( 'Add a new shipping address');
		}
		elseif (JRequest::getVar( 'option' ) == "com_virtuemart" && JRequest::getVar( 'page' ) == "checkout.index" && JRequest::getVar( 'func' ) == "")
		{
			$page = JText::_( 'Checkout');
		}
		elseif (JRequest::getVar( 'option' ) == "com_virtuemart" && JRequest::getVar( 'page' ) == "checkout.index" && JRequest::getVar( 'func' ) == "checkoutProcess" && JRequest::getVar( 'shipping_rate_id' ) == "")
		{
			$page = JText::_( 'Checkout Shipping');
		}
		elseif (JRequest::getVar( 'option' ) == "com_virtuemart" && JRequest::getVar( 'page' ) == "checkout.index" && JRequest::getVar( 'func' ) == "checkoutProcess" && JRequest::getVar( 'shipping_rate_id' ) != "" && JRequest::getVar( 'payment_method_id' ) == 0)
		{
			$page = JText::_( 'Checkout Payment');
		}
		elseif (JRequest::getVar( 'option' ) == "com_virtuemart" && JRequest::getVar( 'page' ) == "checkout.index" && JRequest::getVar( 'func' ) == "checkoutProcess" && JRequest::getVar( 'payment_method_id' ) != 0)
		{
			$page = JText::_( 'Checkout Confirmation');
		}
		elseif (JRequest::getVar( 'option' ) == "com_virtuemart" && JRequest::getVar( 'page' ) == "shop.savedcart")
		{
			$page = JText::_( 'Save Cart');
		}
		elseif (JRequest::getVar( 'option' ) == "com_virtuemart" && JRequest::getVar( 'page' ) == "shop.product_details" && JRequest::getVar( 'product_id' ) != "")
		{
			$product_id = JRequest::getVar( 'product_id' );
			$rows	= modGavirtuemartHelper::getProduct($params, $product_id);
			foreach ( $rows as $row )
				{
					$product_name = $row->product_name;
				}
			$page = JText::sprintf( 'Product', $product_name );
		}
		elseif (JRequest::getVar( 'option' ) == "com_virtuemart" && JRequest::getVar( 'page' ) == "shop.ask" && JRequest::getVar( 'product_id' ) != "")
		{
			$product_id = JRequest::getVar( 'product_id' );
			$rows	= modGavirtuemartHelper::getProduct($params, $product_id);
			foreach ( $rows as $row )
				{
					$product_name = $row->product_name;
				}
			$page = JText::sprintf( 'Product Question', $product_name );
		}
		elseif (JRequest::getVar( 'option' ) == "com_virtuemart" && JRequest::getVar( 'page' ) == "shop.browse" && JRequest::getVar( 'category_id' ) != "")
		{
			$category_id = JRequest::getVar( 'category_id' );
			$rows	= modGavirtuemartHelper::getCategory($params, $category_id);
			foreach ( $rows as $row )
				{
					$category_name = $row->category_name;
				}
			$page = JText::sprintf( 'Category', $category_name );
		}
		elseif (JRequest::getVar( 'option' ) == "com_virtuemart" && JRequest::getVar( 'page' ) != "")
		{
			$page = JText::_( 'Shop');
		}
		elseif (JRequest::getVar( 'option' ) == "com_user" && JRequest::getVar( 'view' ) == "reset")
		{
			$page = JText::_( 'Lost password');
		}
		elseif (JRequest::getVar( 'option' ) == "com_user" && JRequest::getVar( 'view' ) == "remind")
		{
			$page = JText::_( 'Lost Username');
		}
		
		if ($page != "") return "pageTracker._trackPageview(\"".$page."\");"; else return "pageTracker._trackPageview();";
		
	}
	
	//return an order information
	function getOrder($params)
	{
	global $mainframe;
	
	//thank you HOTKEY33 
	if (JRequest::getVar( 'option' ) == "com_virtuemart" && (JRequest::getVar( 'page' ) == "checkout.thankyou" || JRequest::getVar( 'page' ) == "checkout.result" || JRequest::getVar( 'page' ) == "checkout.paybox_result" || JRequest::getVar( 'page' ) == "checkout.spplus_response" || JRequest::getVar( 'page' ) == "checkout.sips_response") )
		{
			$db	=& JFactory::getDBO();
			$user = & JFactory::getUser();
			$user_id = $user->get('id');
			//prefix table for virtuemart
			$table_prefix = $params->get( 'table_prefix', "vm" );
			$queryb = "SELECT city, state, country, user_info_id FROM #__".$table_prefix."_user_info WHERE user_id = " . (int)$user_id . "";
			$db->setQuery( $queryb );
			//user info
			if ($userinfo = $db->loadObjectList()) {
    		foreach ($userinfo as $info) {
				$city = $info->city;
				$country = $info->country;
				$state = $info->state;
				$userinfoid = $info->user_info_id;
				}
			}
			
			//fetch latest order_id for this user	
			$ordersql = "SELECT MAX(order_id) AS order_id FROM #__".$table_prefix."_orders WHERE user_info_id = '" . $userinfoid . "' AND user_id = " . (int)$user_id . "";
			$db->setQuery( $ordersql );
			if($rows = $db->loadObjectList()){
				foreach ( $rows as $row ) {
 				$maxorderid = $row->order_id;
 				}
			}
			
			// fetch order number, order total, order tax	
			$sql = "SELECT order_id, order_total, order_tax, ship_method_id, order_shipping, order_shipping_tax FROM #__".$table_prefix."_orders WHERE user_id = " . (int)$user_id . " AND user_info_id = '" . $userinfoid . "' AND order_id = " . (int)$maxorderid . "";
			$db->setQuery( $sql );
			if ($orderbd = $db->loadObjectList()) {
	    			foreach ($orderbd as $orderinfo) {
						$orderid = $orderinfo->order_id;
						$ordertotal =$orderinfo->order_total;
						$ordertax =$orderinfo->order_tax;
						$ship_method_id =$orderinfo->ship_method_id;
						$shippingsum =$orderinfo->order_shipping;
						$shippingtaxsum = $orderinfo->order_shipping_tax + $orderinfo->order_tax; 
	    			}
	   		}
			
			if($state == "-") $state= JText::_( 'No State'); else $state;
			
			return  '
			pageTracker._initData();
			pageTracker._trackPageview();
			pageTracker._addTrans(
		    "'.$maxorderid.'",                           
		    "'.modGavirtuemartHelper::cleanDoubleQuote($mainframe->getCfg( 'sitename' )).'",                            
		    "'.$ordertotal.'",                                    
		    "'.$shippingtaxsum.'",                                   
		    "'.$shippingsum.'",                                       
		    "'.modGavirtuemartHelper::cleanDoubleQuote($city).'",                                 
		    "'.modGavirtuemartHelper::cleanDoubleQuote($state).'",                               
		    "'.modGavirtuemartHelper::cleanDoubleQuote($country).'"                                
		  	);
		  	'.modGavirtuemartHelper::getProductsOrder($params, $maxorderid, $userinfoid).'
		  	pageTracker._trackTrans();';
			
			
		}
	}
	
	function getProductsOrder ($params, $order_id, $userinfoid)
	{
		$db	=& JFactory::getDBO();
		// fetch cart products
		$productrow = "";
		//prefix table for virtuemart
		$table_prefix = $params->get( 'table_prefix', "vm" );
		$productsdb = "SELECT product_id, order_item_sku, order_item_name, product_item_price, product_final_price, product_quantity FROM #__".$table_prefix."_order_item WHERE user_info_id = '" . $userinfoid . "' AND order_id = " . (int)$order_id . "";
		$db->setQuery( $productsdb );
		$category='';
		
		if ($productsorder = $db->loadObjectList()) {
	    		foreach ($productsorder as $productinfo) {
		
					$product_id = $productinfo->product_id;
	    			$product_sku = $productinfo->order_item_sku; 				
					$product_name = $productinfo->order_item_name;			
					$product_price = $productinfo->product_item_price;
					$quantity = $productinfo->product_quantity;
					// search category
					$catsql = "SELECT DISTINCT (p.category_name) as catname FROM #__".$table_prefix."_product_category_xref c, jos_".$table_prefix."_category p WHERE product_id = " . $product_id ." AND p.category_id = c.category_id";
					$db->setQuery( $catsql );
				
					if ($catrows = $db->loadObjectList()) {
	    				foreach ($catrows as $catrow) {
							$category .= $catrow->catname . " ";
						}
					}
				
					$productrow .= 'pageTracker._addItem(
				    "'.$order_id.'",                             
				    "'.modGavirtuemartHelper::cleanDoubleQuote($product_sku).'",                                     
				    "'.modGavirtuemartHelper::cleanDoubleQuote($product_name).'",                               
				    "'.modGavirtuemartHelper::cleanDoubleQuote($category).'",                          
				    "'.$product_price.'",                                    
				    "'.$quantity.'"                                         
				  	);';
					
					
				}
			}
		return $productrow;
		
	}
	
	//return a product information
	function getProduct($params, $product_id)
	{
		$db	=& JFactory::getDBO();
		//prefix table for virtuemart
		$table_prefix = $params->get( 'table_prefix', "vm" );
		$product_id = JRequest::getVar( 'product_id' );
		$query = "SELECT * FROM #__".$table_prefix."_product WHERE product_id = ".(int)$product_id;
		$db->setQuery($query);
		return $db->loadObjectList();
	}
	
	//return a category information
	function getCategory($params, $category_id)
	{
		$db	=& JFactory::getDBO();
		//prefix table for virtuemart
		$table_prefix = $params->get( 'table_prefix', "vm" );
		$query = "SELECT * FROM #__".$table_prefix."_category WHERE category_id = ".(int)$category_id;
		$db->setQuery($query);
		return $db->loadObjectList();
	}

	//clean double quote
	function cleanDoubleQuote($string)
	{
		return htmlspecialchars($string, ENT_QUOTES, "UTF-8");
	}
}
